// pages/fabu_management/fabu_management.js
const app = getApp(), core = require('../../utils/core');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {type:0, status:1},
    array: ['全部','急招', '直招', '店铺招聘', '红包招聘'],
    show:true,
    jobs: [],
    showPromoteTip: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.refresh();
    app.eventBus.on(app.Events.JobCreate, this.eventOnJobCreate, this);
    app.eventBus.on(app.Events.JobUpdate, this.eventOnJobUpdate, this);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.eventBus.off(app.Events.JobCreate, this.eventOnJobCreate);
    app.eventBus.off(app.Events.JobUpdate, this.eventOnJobUpdate);
  },
  bindTypeChange: function (e) {
    this.setData({
      'query.type': e.detail.value,
      show:false
    });
    this.refresh();
  },
  bindStatusChange:function(e) {
    this.setData({
      'query.status': e.currentTarget.dataset.status
    });
    this.refresh();
  },

  actionTip() {
    this.setData({showActionTip: !this.data.showActionTip});
  },
  bindAddJob: function() {
    core.post({url:'xilujob.job_manage/action_msg',data:{},success:ret=>{
      //到面试邀请页面
      wx.navigateTo({
        url: '/pages/post/post',
      });
    },fail:ret=>{
      wx.showModal({
        title: "提示",
        content: ret.msg,
        success(res){
          if(res.confirm){
            wx.navigateTo({
              url: '/pages/taocan/taocan',
            })
          }
        }
      })
      return false;
    }});
  },
  bindEditJob: function (event) {
    wx.navigateTo({url: '/pages/post/post?id='+event.currentTarget.dataset.id});
  },
  bindDelJob: function (event) {
    let jobId = event.currentTarget.dataset.id, index = event.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否确认删除职位？',
      success: res=>{
        if (res.confirm) {
          core.post({
            url: '/job_manage/del/id/'+jobId,
            success: ret => {
              this.data.jobs.splice(index, 1);
              this.setData({jobs: this.data.jobs});
            }
          });
        }
      }
    });
  },
  bindSwitchOnJob: function (event) {
    let jobId = event.currentTarget.dataset.id, index = event.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否确认发布职位？',
      success: res => {
        if (res.confirm) {
          core.post({
            url: '/xilujob.job_manage/switch_on/id/'+jobId,
            success: ret => {
              let status = ret.data.status;
              if(this.data.query.status!=status) {
                this.setData({'query.status':status});
                this.refresh();
              }else {
                this.data.jobs[index].status = status;
                this.setData({jobs: this.data.jobs});
              }
            }
          })
        }
      }
    });
  },
  bindSwitchOffJob: function (event) {
    let index = event.currentTarget.dataset.index, off_job = event.currentTarget.dataset.off_job;
    wx.showModal({
      title: '提示',
      content: off_job ? '是否下架招聘职位？':'是否取消发布审核？',
      success: res => {
        if (res.confirm) {
          core.post({
            url: '/xilujob.job_manage/switch_off/id/'+this.data.jobs[index].id,
            success: ret => {
              this.data.jobs.splice(index, 1);
              this.setData({jobs: this.data.jobs});
            }
          })
        }
      }
    });
  },
  refresh: function () {
    this.setData({jobs:[]});
    core.get({
      url: '/xilujob.job_manage/index', data: this.data.query, success: ret => {
        this.setData({jobs: ret.data.jobs, jobCount: ret.data.job_count, jobLimit: ret.data.job_limit});
      }
    });
  },
  bindRefreshJob(event) {
    let id = event.currentTarget.dataset.id;
    core.post({url:'xilujob.job_manage/refresh_job',data:{id},success:ret=>{
      wx.showToast({title: '刷新成功',icon:'none'});
    }});
  },
  bindPreviewJob(event) {
    wx.navigateTo({
      url: '/pages/jobs_detail/jobs_detail?id='+event.currentTarget.dataset.id
    })
  },
  bindDuplicateJob(event) {
    wx.showModal({
      title: '提示',
      content: '是否要复制此职位？',
      success: res=>{
        if (res.confirm) {
          let id = event.currentTarget.dataset.id;
          core.post({url:'job_manage/duplicate_job',data:{id},success:ret=>{
            let job = ret.data.job;
            this.eventOnJobCreate(job);
          }});
        }
      }
    });
  },
  bindEditJobTip() {
    wx.showModal({
      title: '提示',
      content: '暂停后在未发布状态修改编辑',
      showCancel:false,
    })
  },
  bindPromote() {
    this.setData({showPromoteTip:true});
  },
  hidePromoteTip() {
    this.setData({showPromoteTip:false});
  },
  share(e) {
    core.get({url:'xilujob.job/poster_info/job_id/'+e.currentTarget.dataset.id, success:ret=>{
      core.setCache('temp', ret.data,10);
      wx.navigateTo({
        url: '/pages/share_post_new/share_post_new'
      });
    }});
  },
  eventOnJobCreate: function (job) {
    let status = job.status;
    if(this.data.query.status!=status) {
      this.setData({'query.status':status});
      this.refresh();
    }else {
      this.data.jobs.splice(0,0, job);
      this.setData({jobs: this.data.jobs,jobCount:this.data.jobCount+1});
    }
  },
  eventOnJobUpdate: function (job) {
    for(let i=0;i<this.data.jobs.length;i++) {
      if(job.id==this.data.jobs[i].id) {
        this.data.jobs.splice(i, 1, job);
        this.setData({jobs: this.data.jobs});
        break;
      }
    }
  }
});
